using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Resources;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
using Microsoft.SharePoint;
using System.ComponentModel;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.IO;
using System.Xml.Serialization;
using IWCommunity.Common;
using IWCommunity.Blogs.WebControls;

namespace IWCommunity.WebControls
{

    /// <summary>
    /// Web part which just is a layer on the RollUpList web control.
    /// </summary>
    [ToolboxData("<{0}:RollupWebPart runat=server></{0}:RollupWebPart>")]
    [XmlRoot(Namespace = "IWCommunity.Blogs.WebControls")]
    public class RollupWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {

        private string _sites = string.Empty;
        private ReportViewType _reportView = ReportViewType.LatestPosts;
        private bool _includeSubsites = false;
        private int _maxSiteDepth = 1;
        private int _maxDisplayItems = 5;


        #region Properties

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [Browsable(true), Category("Miscellaneous"), WebPartStorage(Storage.Shared), FriendlyName("Report View"), Description("The view of data to display")]
        public ReportViewType ReportView
        {
            get { return _reportView; }
            set { _reportView = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [Browsable(true), Category("Miscellaneous"), WebPartStorage(Storage.Shared), FriendlyName("Sites"), Description("Sites")]
        public string Sites
        {
            get
            {
                return _sites;
            }
            set
            {
                _sites = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [Browsable(true), Category("Miscellaneous"), WebPartStorage(Storage.Shared), FriendlyName("Include Sub Sites"), Description("Include Sub Sites")]
        public bool IncludeSubsites
        {
            get { return _includeSubsites; }
            set { _includeSubsites = value; }
        }


        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [Browsable(true), Category("Miscellaneous"), WebPartStorage(Storage.Shared), FriendlyName("Max Site Depth"), Description("Max Site Depth")]
        public int MaxSiteDepth
        {
            get { return _maxSiteDepth; }
            set { _maxSiteDepth = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [Browsable(true), Category("Miscellaneous"), WebPartStorage(Storage.Shared), FriendlyName("Max Display Items"), Description("Max Display Items")]
        public int MaxDisplayItems
        {
            get { return _maxDisplayItems; }
            set { _maxDisplayItems = value; }
        }

        private string _xslFileName;
        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [Browsable(true), Category("Miscellaneous"), WebPartStorage(Storage.Shared), FriendlyName("XSL File Name"), Description("Name of the file to use for XSTL tranformation")]
        public string XslFileName
        {
            get
            {
                return _xslFileName;
            }

            set
            {
                _xslFileName = value;
            }
        }

        private int _cacheTimeout;
        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [Browsable(true), Category("Miscellaneous"), WebPartStorage(Storage.Shared), FriendlyName("Cache Timeout"), Description("How long (mins) to cache the result HTML for")]
        public int CacheTimeout
        {
            get
            {
                return _cacheTimeout;
            }

            set
            {
                _cacheTimeout = value;
            }
        }

        private bool _enableCache;
        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [Browsable(true), Category("Miscellaneous"), WebPartStorage(Storage.Shared), FriendlyName("Enable Cache"), Description("Set to true to enable caching of the html")]
        public bool EnableCache
        {
            get
            {
                return _enableCache;
            }

            set
            {
                _enableCache = value;
            }
        }

        private bool _enableCacheDebug = false;
        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [Browsable(true), Category("Miscellaneous"), WebPartStorage(Storage.Shared), FriendlyName("Show Cache Debug"), Description("Adds date and time rendered to part")]
        public bool EnableCacheDebug
        {
            get
            {
                return _enableCacheDebug;
            }

            set
            {
                _enableCacheDebug = value;
            }
        }

        #endregion

        
        protected override void CreateChildControls()
        {
            RollUpList rollList = new RollUpList();
            //set all the appropriate properties
            rollList.ID = "rollList";
            rollList.ReportView = _reportView;
            rollList.CacheTimeout = _cacheTimeout;
            rollList.EnableCaching = _enableCache;
            rollList.CacheDebug = _enableCacheDebug;
            rollList.Transform.XslName = XslFileName;
            this.Controls.Add(rollList);

            base.CreateChildControls();
        }
    }
}
